<style type="text/css">
	.checkbox{
		min-width:80px;
		margin-right:10px;
		display:inline-block;
		
		
		
	}
</style>

<? include 'order_head.php'?>


<h2 class="headSection">จัดส่งสินค้า</h2>
<table cellpadding="6" cellspacing="1"  border="0" class="myTable table2">

<tr>
  
  <th style="width:45%">สินค้า</th>
  <th style="text-align:center;width:10%">จำนวน</th>
  
  <th style="text-align:right;width:10%">มูลค่า</th>
  
</tr>

<?php $i = 1; ?>
<?
$allDiscount = 0;
$totalSale = 0;
$totalPremium = 0;
$premiumPrice = 0;
$firstSendPrice = 0;
?>

<?php foreach ($this->cart->contents() as $items): ?>

	<?
	
	$option = $this->cart->product_options($items['rowid']);
	
	$option = (array)$option;
	$pID = $option['MATNR'];
	$pName = $option['MAKTX'];
	$baseUnit = $option['BASE_UNIT'];
	if($baseUnit=="KAR"){
		$b = "ลัง";
	}else if($baseUnit=="PAK"){
		$b = "แพ็ก";
	}else {
		$b = "ชิ้น";
	}
	?>
	<?
	$class="";
	$pType = "sale";
	if(isset($option['ISPremium'])){
		if($option['ISPremium']=="Yes"){
			$class = "line-preimum";
			$totalPremium+=$items['subtotal'];
			$pName = $pName."<br>(สินค้าแถม)";
			$pType = "premium";	
		}
		
	}else{
		$totalSale+=$items['subtotal'];
	}
	$sendQty = $items['qty'] - ($items['qty'] - $DI[$i-1]['pQty']);
	$sendPrice = $sendQty * $items['price'];
	if($sendQty > 0 && $pType=="sale" ){
		$firstSendPrice += $sendPrice;
	}else if($sendQty > 0 && $pType=="premium" ){
		$premiumPrice += $sendPrice;
	}	
	if($sendQty > 0 ){
	?>
	<tr class="<?php echo $class?>">
		
	  <td style="width:40%"><?php echo $pID ?><br />
		<?php echo $pName ?></td>
		<?
		$items['qty'] = $sendQty;
		?>
		<td style="width:10%;text-align: right;"><?php echo converUnit($items,$option);?></td>
		 
		
	
		
	  <td style="text-align:right;width:60px">
	  	<div style="width:60px;float:right;">
	  		<?php echo number_format($sendPrice,2) ?>
	  		</div>
	  </td>
	  
	  
	  
	  
	</tr>
	
<?php }
	$i++;
?>

<?php endforeach; ?>

<tr>
  <td colspan=""> </td>
  <td class="right"><strong>ราคารวม</strong></td>
  
  <td class="right" style="text-align: right"><?php echo $this->cart->format_number($firstSendPrice); ?></td>
</tr>
<tr>
  <td colspan=""> </td>
  <td class="right"><strong>ราคาของแถม</strong></td>
  
  <td class="right" style="text-align: right"><?php echo $this->cart->format_number($premiumPrice); ?></td>
</tr>
<tr>
  <td colspan=""> </td>
  <td class="right"><strong>ส่วนลด</strong></td>
  <td class="right" style="text-align: right"><?php echo $this->cart->format_number($discount); ?></td>
</tr>
<tr>
  <td colspan=""> </td>
  <td class="right"><strong>สุทธิ</strong></td>
  <td class="right" style="text-align: right"><?php echo $this->cart->format_number($firstSendPrice - $discount); ?></td>
</tr>

</table>
<br /> 
<h2>วิธีการชำระเงิน</h2>
<?

?>

<br />



<div class="paymentBock">
<form action="<?php echo base_url()?>order/viewordersummary/" id="paymentForm" method="post">
	<p>
	<input name="paymentStatus" value="Y" type="radio" checked="checked">ชำระแล้ว
	<input name="paymentStatus" value="N" type="radio">ยังไม่ชำระ
	</p>
	<p>
	<span class="checkbox">วันที่จัดส่ง</span>
	<input class="shipDate" readonly="readonly" type="text"  name="shipDate" value="<?php echo date("Y-m-d")?>"> 
	</p>
	
	<p>
	<span class="checkbox"><input type="checkbox" checked="checked" >เงินสด</span>
	<input class="checkQty" type="text"  name="cash_amount" value="<?php echo str_replace(",", "", number_format($firstSendPrice - $discount,2))?>"> บาท
	</p>
	
	<p>
	<span class="checkbox"><input type="checkbox">เช็ค</span>
	
	<input class="checkQty" type="text"  name="amount_cheque"> บาท<br />
	<p>
	<span class="checkbox">ธนาคาร</span><input type="text" name="bank_cheque"> 
	<span class="checkbox">สาขา</span><input type="text" name="branch_cheque"><br /><br />
	<span class="checkbox">วันที่</span><input type="text" name="date_cheque">
	
	</p>
	</p>
	<p>
	<span class="checkbox"><input type="checkbox">โอนเงิน</span>
	<input class="checkQty" type="text"  name="amount_trans"> บาท<br />
	<p>
	<span class="checkbox">ธนาคาร</span><input type="text" name="bank_trans"> 
	<span class="checkbox">สาขา</span><input type="text" name="branch_trans"><br /><br />
	<span class="checkbox">วันที่</span><input type="text" name="date_trans">
	
	</p>
	</p>
	<p>
	<a class="btn-default" href="<?php echo base_url()?>order/viewallorder/"><i class="fa fa-chevron-left"></i> ย้อนกลับ</a>
	<button type="submit" class="btn-next" id="check" ><i class="fa fa-chevron-right"></i> สรุปรายการสั่งซื้อ</button>
</form>
</div>
<script type="text/javascript">
	$(function(){
		$( ".shipDate" ).datepicker({dateFormat:'yy-mm-dd'});
	})
</script>
<script type="text/javascript">
	$(function(){
		$(document).ready(function() {
    $(".checkQty").keydown(function (e) {
        // Allow: backspace, delete, tab, escape, enter and .
        if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
             // Allow: Ctrl+A, Command+A
            (e.keyCode == 65 && ( e.ctrlKey === true || e.metaKey === true ) ) || 
             // Allow: home, end, left, right, down, up
            (e.keyCode >= 35 && e.keyCode <= 40)) {
                 // let it happen, don't do anything
                 return;
        }
        // Ensure that it is a number and stop the keypress
        if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
            e.preventDefault();
        }
    });
});
		$('#check').click(function(){
			var process = true;
			var message = "";
			var maxCheck = <?php echo str_replace(",", "", number_format($firstSendPrice - $discount,2))?>;
			var sum = 0;
			$.each($('.checkQty'),function(){
				var val =  $(this).val();
				if(val!=""){
					sum += parseFloat(val);
				}
			})
			
			if(sum != maxCheck){
					process = false;
					message = "มูลค่าที่ชำระต้องมีค่าเท่ากับมูลค่าจัดส่งสินค้า";
			}
			if(!process){
				alert(message);
				return false;	
			}
			
		})
		
		
	})
</script>